/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mapeClient;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author marco
 */
public class TCPServiceProxy implements ServiceProxy{
    
    private Socket socket;
    private PrintWriter send;

  
    private String address;
    private int port;
    
    public TCPServiceProxy(String address, int port){
        this.setAddress(address);
        this.setPort(port);
    }
    
    @Override
    public void enqueue(String event) {
        try {
            
            this.socket = new Socket(address, port);
            send = new PrintWriter(socket.getOutputStream(),true);
            
            send.print(event);
        
                       
        } catch (UnknownHostException ex) {
            Logger.getLogger(TCPServiceProxy.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(TCPServiceProxy.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            try {
                send.close();
                socket.close();
            } catch (IOException ex) {
                Logger.getLogger(TCPServiceProxy.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
          
    }
    
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }
       
}
